package hot;

public class Num312 {
    public int maxCoins(int[] nums) {
        int n  =nums.length;
        int[] vals = new int[n + 2];
        vals[0] = vals[n + 1] = 1;
        for(int i = 1; i <= n; i++) {
            vals[i] = nums[i - 1];
        }
        int[][] dp = new int[n + 2][n + 2];
        for(int i = n - 1; i >= 0; i--) {
            for(int j = i + 2; j < n + 2; j++) {
                for(int k = i + 1; k < j; k++) {
                    int value = vals[i] * vals[k] * vals[j];
                    value += dp[i][k] + dp[k][j];
                    dp[i][j] = Math.max(dp[i][j],value);
                }
            }
        }
        return dp[0][n + 1];
    }
}
